package com.jxau.controller;

import java.util.List;

import com.jxau.entity.Comment;
import com.jxau.entity.Responses;
import com.jxau.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

import com.github.pagehelper.PageInfo;
import com.jxau.entity.Temp;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping("/addCommentUnderArticle")
    public Responses addCommentUnderArticle(Comment comment) {
        Comment newComment = commentService.addCommentUnderArticle(comment);
        return Responses.success("添加文章下评论成功！", newComment);
    }

    @PostMapping("/addCommentReply")
    public Responses addCommentReply(Comment comment) {
        Comment newComment = commentService.addCommentReply(comment);
        return Responses.success("添加子回复成功！",newComment);
    }

    @GetMapping("/queryMyComment")
    public Responses queryMyComment(String uid,@RequestParam(defaultValue = "1") int nowPage) {
        ArrayList<Comment> comments = commentService.queryMyComment(uid,nowPage);
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        return Responses.success("查询我的评论成功！", comments,pageInfo.getPages());
    }

    @GetMapping("/queryCommentOfUser")
    public Responses queryCommentOfUser(String uid,@RequestParam(defaultValue = "1") int nowPage) {
        ArrayList<Comment> comments = commentService.queryCommentOfUser(uid,nowPage);
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        return Responses.success("查询我的评论成功！", comments,pageInfo.getPages());
    }
    @GetMapping("/queryCommentOfArticle")
    public Responses queryCommentOfArticle(int articleId,String uid,@RequestParam(defaultValue = "1")int nowPage){
        ArrayList<Comment> comments = commentService.queryCommentOfArticle(articleId,uid,nowPage);
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        return Responses.success("查询文章下评论成功！",comments,pageInfo.getPages());
    }
    @PutMapping("/cleanRedPointOfComment")
    public Responses cleanRedPointOfComment(String uid) {
        commentService.cleanRedPointOfComment(uid);
        return Responses.success("清除评论红点成功！");
    }
//--------------------------------------------------------------------------------

    @RequestMapping("/findComment")
    public List<Temp> findComment(String name) {
        return commentService.findCommentByName(name);
    }

    @RequestMapping("/deleteComment")
    public int deleteCommentById(int id) {
        return commentService.deleteCommentById(id);
    }

    @RequestMapping("/page")
    public List<Temp> getPages(int pageNum, int pageSize) {
        List<Temp> articles = commentService.getPages(pageNum, pageSize);
        return articles;
    }

    /**
     * 获取分页条的相关信息
     *
     * @return
     */
    @RequestMapping("/pageData")
    public PageInfo<Temp> getPagesData() {
        return commentService.getPagesData();
    }

}
